home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / bbs / wildcat / install.c < prev    next >
C/C++ Source or Header  |  1993-04-19  |  8KB  |  385 lines

  1. /*
  2.  * install.c
  3.  * -force option
  4.  *
  5.  * cica version
  6.  */
  7.  
  8. #include <ctype.h>
  9. #include <conio.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #if 0
  17.   FileAreaRecType = record
  18.                       AreaName       : Str30;
  19.                       AreaPath       : array[1..4] of DirStr;
  20.                     end;
  21.  
  22. #endif
  23.  
  24. // don't pack structures
  25. #pragma -a-
  26. struct file_area_rec_type {
  27.     unsigned char length;
  28.     char area_name[30];
  29.     struct {
  30.         unsigned char length;
  31.         char path[67];
  32.     } area_path[4];
  33. };
  34.  
  35. struct make_wild_record {
  36.     char dummy1[0x321];
  37.     int max_file_areas;
  38. };
  39.  
  40. char letter;
  41. int start_area;        /* xxx */
  42.  
  43. int
  44. highest_area(void) {
  45.     char buf[500];
  46.     int fd;
  47.     int i;
  48.     struct file_area_rec_type area;
  49.     int count = 0;
  50.  
  51.     if (-1 == (fd = open("filearea.dat", O_RDWR | O_BINARY))) {
  52.         fprintf(stderr, "unable to open 'filearea.dat'\n");
  53.         exit(1);
  54.     }
  55.     
  56. //    printf("%d\n", sizeof(struct file_area_rec_type));
  57.  
  58.     for (;;) {
  59.         i = read(fd, &area, sizeof(struct file_area_rec_type));
  60.         if (i == 0)
  61.             break;
  62.  
  63.         if (i != sizeof(struct file_area_rec_type)) {
  64.             fprintf(stderr, "error reading file: (%d) %s\n",
  65.                 i, sys_errlist[errno]);
  66.             exit(1);
  67.         }
  68.         ++count;
  69.         strncpy(buf, area.area_name, area.length);
  70.         if (area.length == 0)
  71.             continue;
  72.         buf[area.length] = 0;
  73. //        printf("area: `%s'\n", buf);
  74.         for (i = 0; i < 4; i++) {
  75.             if (area.area_path[i].length == 0)
  76.                 continue;
  77.             strncpy(buf, area.area_path[i].path,
  78.                     area.area_path[i].length);
  79.             buf[area.area_path[i].length] = 0;
  80. //            printf("path %d: `%s'\n", i, buf);
  81.         }
  82.     }
  83.     
  84.     close(fd);
  85.     
  86.     return(count);
  87. }
  88.  
  89. void
  90. install_areas(void) {
  91.     char vbuf[3000];
  92.     char buf[500];
  93.     int fd;
  94.     int i;
  95.     struct file_area_rec_type area;
  96.     FILE *fh;
  97.     char line[200];
  98.     char description[100];
  99.     char path[100];
  100.     int current_areas;
  101.     char *p;        
  102.  
  103.     if (-1 == (fd = open("filearea.dat", O_RDWR | O_BINARY))) {
  104.         fprintf(stderr, "unable to open 'filearea.dat'\n");
  105.         exit(1);
  106.     }
  107.     
  108.     sprintf(buf, "%c:\\dirs.txt", letter);
  109.     if (NULL == (fh = fopen(buf, "rt"))) {
  110.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  111.             buf, sys_errlist[errno]);
  112.         exit(1);
  113.     }
  114.     
  115.     setvbuf(fh, vbuf, _IOFBF, 2998);
  116.  
  117.     current_areas = 0;
  118.     while (1) {
  119.         i = read(fd, &area, sizeof(struct file_area_rec_type));
  120.         if (i == 0)
  121.             break;
  122.  
  123.         if (i != sizeof(struct file_area_rec_type)) {
  124.             fprintf(stderr, "error reading file: (%d) %s\n",
  125.                 i, sys_errlist[errno]);
  126.             exit(1);
  127.         }
  128.         ++current_areas;
  129.     }
  130.  
  131.     memset(&area, 0, sizeof(struct file_area_rec_type));
  132.     /* write null areas into any gap */
  133.     while (current_areas < start_area - 1) {
  134.         i = write(fd, &area, sizeof(struct file_area_rec_type));
  135.  
  136.         if (i != sizeof(struct file_area_rec_type)) {
  137.             fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  138.                 "filearea.dat", i, sys_errlist[errno]);
  139.             fclose(fh);
  140.             exit(1);
  141.         }
  142.         ++current_areas;
  143.     }
  144.     
  145.     while (NULL != fgets(line, 199, fh)) {
  146.         memset(&area, 0, sizeof(struct file_area_rec_type));
  147.         
  148. //        ++current_areas;
  149.         
  150.         p = strtok(line, "\n\r\t ");
  151.         if (!p) {
  152.             fprintf(stderr, "line has no path:\n``%s''\n",
  153.                 line);
  154.             exit(1);
  155.         }
  156.         
  157.         strcpy(path, p);
  158.         if (line[strlen(path) - 1] == '\\')
  159.             line[strlen(path) - 1] = 0;
  160.  
  161.         p = line + strlen(path) + 1;
  162.         while (isspace(*p))
  163.             ++p;
  164.         if (!p) {
  165.             fprintf(stderr, "line has no description:\n``%s''\n",
  166.                 line);
  167.             exit(1);
  168.         }
  169.         strcpy(description, p);
  170.         *(description + strlen(description) - 1) = 0;
  171.         
  172.         sprintf(buf, "%c:%s\\", letter, line);
  173.         strcpy(area.area_path[0].path, buf);
  174.         area.area_path[0].length = (unsigned char)strlen(buf);
  175.  
  176.         i = strlen(description);
  177.         if (i > 30)
  178.             i = 30;
  179.         fprintf(stderr, "installing: %s\n", description);
  180.         strncpy(area.area_name, description, i);
  181.         area.length = (unsigned char)i;
  182.         
  183.         i = write(fd, &area, sizeof(struct file_area_rec_type));
  184.  
  185.         if (i != sizeof(struct file_area_rec_type)) {
  186.             fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  187.                 "filearea.dat", i, sys_errlist[errno]);
  188.             fclose(fh);
  189.             exit(1);
  190.         }
  191.     }
  192.     
  193.     fclose(fh);
  194.     close(fd);
  195. }
  196.  
  197. int
  198. count_areas(void) {
  199.     char vbuf[8000];
  200.     char buf[500];
  201.     char line[200];
  202.     FILE *fh;
  203.     int areas;
  204.  
  205.     sprintf(buf, "%c:\\dirs.txt", letter);
  206.     if (NULL == (fh = fopen(buf, "rt"))) {
  207.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  208.             buf, sys_errlist[errno]);
  209.         exit(1);
  210.     }
  211.     
  212.     setvbuf(fh, vbuf, _IOFBF, 7998);
  213.  
  214.     areas = 0;
  215.     while (NULL != fgets(line, 199, fh)) {
  216.         ++areas;
  217.     }
  218.     
  219.     fclose(fh);
  220.     
  221.     return(areas);
  222. }
  223.  
  224. void
  225. build_batch(void) {
  226.     char vbuf[3000];
  227.     char vbuf2[3000];
  228.     char buf[500];
  229.     FILE *fh;
  230.     FILE *output;
  231.     char line[200];
  232.     char path[100];
  233.     int a;
  234.     char *p;        
  235.     
  236.     fprintf(stderr, "Building batch file 'install2.bat'... ");
  237.  
  238.     sprintf(buf, "%c:\\dirs.txt", letter);
  239.     if (NULL == (fh = fopen(buf, "rt"))) {
  240.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  241.             buf, sys_errlist[errno]);
  242.         exit(1);
  243.     }
  244.     setvbuf(fh, vbuf, _IOFBF, 2998);
  245.     
  246.     if (NULL == (output = fopen("install2.bat", "wt"))) {
  247.         fprintf(stderr, "error opening \"install2.bat\": %s\n",
  248.             sys_errlist[errno]);
  249.         exit(1);
  250.     }
  251.     setvbuf(output, vbuf2, _IOFBF, 2998);
  252.  
  253.     a = start_area;
  254.     while (NULL != fgets(line, 199, fh)) {
  255.         p = strtok(line, "\n\r\t ");
  256.         if (!p) {
  257.             fprintf(stderr, "line has no path:\n``%s''\n",
  258.                 line);
  259.             exit(1);
  260.         }
  261.         
  262.         strcpy(path, p);
  263.         if (line[strlen(path) - 1] == '\\')
  264.             line[strlen(path) - 1] = 0;
  265.  
  266.         fprintf(output,
  267.             "wcfile /A:%d /S:%c:%s /D:%c:%s\\00_index.txt /U:\"CICA CDROM\" /P:1,12,25,80 /R\n",
  268.         a, letter, path, letter, path);
  269.  
  270.         ++a;
  271.     }
  272.     
  273.     fclose(fh);
  274.     fclose(output);
  275.     fprintf(stderr, "done.");
  276. }
  277.  
  278.  
  279. void
  280. main(char *argv[]) {
  281.     char buf[2000];
  282.     char *p;
  283.     int i;
  284.     int fd;
  285.     int highest_filearea;
  286.     struct make_wild_record rec;
  287.     int force = 0;
  288.     int areas;
  289.     
  290.     if (0 == stricmp(argv[1], "-f"))
  291.         ++force;
  292.  
  293.     highest_filearea = highest_area();
  294.     
  295.     if (-1 == (fd = open("makewild.dat", O_RDWR | O_BINARY))) {
  296.         fprintf(stderr, "unable to open 'makewild.dat'\n");
  297.         exit(1);
  298.     }
  299.     
  300.     i = read(fd, &rec, sizeof(struct make_wild_record));
  301.  
  302.     if (i != sizeof(struct make_wild_record)) {
  303.         fprintf(stderr, "error reading '%s' file: (%d) %s\n",
  304.             "makewild.dat", i, sys_errlist[errno]);
  305.         exit(1);
  306.     }
  307.     
  308.     lseek(fd, 0L, SEEK_SET);
  309.     
  310.     if (rec.max_file_areas != highest_filearea) {
  311.         fprintf(stderr,
  312. "Size of filearea.dat file area database (%d areas) and # areas\n",
  313.             highest_filearea);
  314.         fprintf(stderr, "configured (%d areas) don't match.\n",
  315.             rec.max_file_areas);
  316.         fprintf(stderr, "Set # of new areas with makewild.\n");
  317.         exit(1);
  318.     }
  319.  
  320. again:;
  321.     cprintf("What DOS drive letter is your CDROM drive?\n\r");
  322.     gets(buf);
  323.     p = buf;
  324.     while (isspace(*p))
  325.         ++p;
  326.     letter = *p;
  327.  
  328.     areas = count_areas();
  329.     while (1) {
  330.         cprintf("What area should this CDROM's areas start at?\n\r");
  331.         cprintf("Your highest area # is %d.  This CDROM disc\n\r", 
  332.             highest_filearea);
  333.         cprintf("has %d areas.  A good starting area is %d.\n\r", 
  334.             areas, highest_filearea + 1);
  335.         
  336.         gets(buf);
  337.         p = buf;
  338.         while (isspace(*p))
  339.             ++p;
  340.         
  341.         start_area = atoi(p);
  342.         if (! force && start_area <= highest_filearea) {
  343.           cprintf("Sorry, area %d is already used.  Free areas start\n\r",
  344.               start_area);
  345.             cprintf("with area %d.\n\r", highest_filearea + 1);
  346.             continue;
  347.         }
  348.         
  349.         break;
  350.     }
  351.     
  352.     cprintf("The CDROM drive is DOS drive `%c'.  The new areas will run from\n\r", letter);
  353.     cprintf("area %d to area %d with your last area area %d.\n\r", 
  354.         start_area, start_area + areas, highest_filearea);
  355.     cprintf("If this is correct type 'y'.\n\r");
  356.     
  357.     gets(buf);
  358.     p = buf;
  359.     while (isspace(*p))
  360.         ++p;
  361.     
  362.     if (toupper(*p) != 'Y')
  363.         goto again;
  364.  
  365.     /* write new number of files into global files area */
  366.     rec.max_file_areas = start_area + areas;
  367.     
  368.     i = write(fd, &rec, sizeof(struct make_wild_record));
  369.  
  370.     if (i != sizeof(struct make_wild_record)) {
  371.         fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  372.             "makewild.dat", i, sys_errlist[errno]);
  373.         exit(1);
  374.     }
  375.     
  376.     /* write file descriptions into filearea.dat */
  377.     install_areas();
  378.     
  379.     /* write batch file to add the files */
  380.     
  381.     build_batch();
  382.     
  383.     close(fd);
  384. }
  385.